package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.NpcVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserDialogueNpcEvent;
import com.wan37.logic.service.cache.NpcServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.message.client2server.game.Game;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author : luoyong
 * @date : 2020-06-04 17:03
 **/
public class CGameChatHandler extends MsgHandler {
    NpcServiceCache npcServiceCache;
    RouteUtils routeUtils;
    private static final Logger logger = LoggerFactory.getLogger(CGameChatHandler.class);

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameChatHandler(MessageVO messageVO) {
        super(messageVO);
        this.npcServiceCache = SpringContextHolder.getBean("npcServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
    }

    @Override
    public void execute(Worker worker) throws Exception {

        Game.CGameChatMsg cGameChatMsg = (Game.CGameChatMsg) messageVO.getMessage();

        String userId = cGameChatMsg.getSenderId();
        String npcId = cGameChatMsg.getReceiverId();

        NpcVO npcVO = npcServiceCache.getNpcIdToNpc().get(npcId).getNpcVO();

        routeUtils.sendResponse(Common.GAME_CHAT_SUCCESS, npcVO.getContent(), messageVO.getNetId(), userId);
        EventDispatcher.getInstance().fireEvent(new UserDialogueNpcEvent(EventType.DIALOGUE_NPC, userId, npcId));
        logger.info("请求对话NPC成功！！！");

    }
}
